package com.hopes.bizz.risk.control.support;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.hopes.bizz.risk.control.vo.RiskDetailExcelVO;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yumr
 * @ProjectName sd-sfes-web
 * @Description 风险管控清单单元格合并策略
 * @time 2023/6/16 11:19
 */
public class RiskDetailedMergeStrategy extends AbstractMergeStrategy {

    private Sheet sheet;
    private final List<RiskDetailExcelVO> riskDetailExcelVOList;

    public RiskDetailedMergeStrategy(List<RiskDetailExcelVO> riskDetailExcelVOList) {
        this.riskDetailExcelVOList = riskDetailExcelVOList;
    }

    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
        this.sheet = sheet;
        switch (cell.getColumnIndex()) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: {
                this.mergeGroupColumn(RiskDetailExcelVO::getRiskObjectId, cell.getColumnIndex());
                break;
            }
            case 7: case 8: case 9: case 10: case 11: {
                this.mergeGroupColumn(RiskDetailExcelVO::getRiskUnitId, cell.getColumnIndex());
                break;
            }
            case 12: case 13: case 14: case 15: case 16: case 17: case 18: {
                this.mergeGroupColumn(RiskDetailExcelVO::getUnitEventId, cell.getColumnIndex());
                break;
            }
            case 19: case 20: case 21: case 22: case 23: case 24: case 25: {
                this.mergeGroupColumn(RiskDetailExcelVO::getControlMeasureId, cell.getColumnIndex());
                break;
            }
            case 26: {
                this.mergeGroupColumn(RiskDetailExcelVO::getTaskCheckContentId, cell.getColumnIndex());
                break;
            }
            default:
                break;
        }
    }

    private void mergeGroupColumn(Function<RiskDetailExcelVO, Long> mapper, Integer column) {
        final LinkedHashSet<Long> idSet = riskDetailExcelVOList.parallelStream().map(mapper).filter(Objects::nonNull).collect(Collectors.toCollection(LinkedHashSet::new));
        final Map<Long, Long> collect = riskDetailExcelVOList.stream().filter(e -> Objects.nonNull(mapper.apply(e))).collect(Collectors.groupingBy(mapper, Collectors.counting()));
        int rowCnt = 2;
        for (Long id : idSet) {
            final int count = collect.get(id).intValue();
            if (count == 1) {
                rowCnt += count;
                continue;
            }
            final CellRangeAddress cellAddresses = new CellRangeAddress(rowCnt, rowCnt + count - 1, column, column);
            sheet.addMergedRegionUnsafe(cellAddresses);
            rowCnt += count;
        }
    }
}
